// mixins/global.js
export const globalMixin = {
	// 生命周期钩子 - 所有组件都会触发
	onLoad() {
		// 页面加载时记录来源页面
		this.fromPage = getCurrentPages()[getCurrentPages().length - 2]?.route;
		const backPage = uni.getStorageSync('currentPage')
		if (!backPage && this.fromPage) {
			uni.setStorageSync('currentPage', this.fromPage)
		}
		console.log('页面加载:', this.fromPage);
	},

	// 注入公共方法
	methods: {
		leftClick() {
			const backPage = uni.getStorageSync('currentPage')
			if (backPage && !this.fromPage) {
				uni.removeStorageSync('currentPage')
				uni.redirectTo({
					url: `/${backPage}`,
					fail() {
						uni.switchTab({
							url: `/${backPage}`
						})
					}
				})
			} else {
				uni.navigateBack()
			}
		},
		setTabbarLang() {
			const langs = [this.$t('index.home'), this.$t('index.found'), this.$t('index.cart'), this.$t(
				'index.my')]
			for (let i = 0; i < 4; i++) {
				uni.setTabBarItem({
					index: i,
					text: langs[i]
				});
			}
		},
		getLang() {
			const lang = uni.getStorageSync('hhh-lang') || 'en'
			if (lang) {
				this.$i18n.locale = lang
				uni.setLocale(lang)
			}
		}
	},
};